<?php


class Test_Spider {
    
    private $_spider = null;

	public function __construct() {
        
        V::addLibrary('third', dirname(V_PATH).'/third-lib/');
        
        $config = array(
            'grabber' => array(
                'engine' => 'curl',
                'option' => array(
                    // 使用文件作为缓存
                    'cache' => array(
                        'engine' => 'File',
                        'option' => array(
                            'cache_dir_level' => 3,
                        ),
                    ),
                    'cache_time' => 3600,
                    'timeout' => 30,
                    'user_agent' => array(
                        'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
                        'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0',
                    ),
                ),
            ),
        );
        
        $this->_spider = new Spider($config);
	}
    
    public function testGrabLinks() {

        $url = 'https://www.baidu.com/';
        $links = $this->_spider->fetchLinks($url);
        echo 'fetch links of ', $url, PHP_EOL;
        print_r($links);
        echo PHP_EOL;
        return $links ? true : false;
    }

    public function testGrabEntity() {

        $entityOption = array(
            'charset' => 'UTF-8',
            'rules' => array(
                'description' => array(
                    'selector' => array('meta[name=description]', 0),
                    'attr' => 'content',
                ),
                'pics'   => array(
                    'selector' => 'img',
                    'attr'     => 'src',
                ),
            ),
        );
        $url = 'https://www.baidu.com/';
        $data = $this->_spider->fetchEntity($entityOption, $url);
        echo 'fetch entity of ', $url, PHP_EOL;
        print_r($data);
        echo PHP_EOL;
        return $data ? true : false;
    }
}